import sys
import os
from pathlib import Path
import site

def patch_paddle_paths():
    """全面处理Paddle在PyInstaller环境下的路径问题"""
    meipass = Path(sys._MEIPASS)
    
    # 1. 重写getsitepackages
    def new_getsitepackages():
        return [
            str(meipass),
            str(meipass / 'lib'),
            str(meipass / 'paddle'),
            str(meipass / 'paddle' / 'libs')
        ]
    site.getsitepackages = new_getsitepackages
    
    # 2. 设置用户site路径
    # site.USER_SITE = str(meipass / 'user_site')
    
    # 3. 直接设置环境变量(备用方案)
    lib_dir = meipass / 'paddle' / 'libs'
    if 'LD_LIBRARY_PATH' in os.environ:
        os.environ['LD_LIBRARY_PATH'] = f"{lib_dir}:{os.environ['LD_LIBRARY_PATH']}"
    else:
        os.environ['LD_LIBRARY_PATH'] = str(lib_dir)

# 应用补丁
patch_paddle_paths()

# 确保Paddle导入前补丁已生效
import paddle
